<template>
  <div class="scroll" @click="scrollTop" title="回到顶部" v-if="showTop">
    <i class="iconfont icon-up"></i>
  </div>
</template>

<script>
export default {
  data: function () {
    return {
      timer: null
    };
  },
  props: ['showTop', 'domId'],
  methods: {
    scrollTop () {
      if (this.timer) {
        clearInterval(this.timer)
      }
      this.timer = setInterval(() => {
        let doc = this.domId ? document.getElementById(this.domId) : (document.documentElement || document.body);
        let win = this.domId ? document.getElementById(this.domId) : window;
        let scrollTop =
          doc.scrollTop || doc.scrollTop;

        if (scrollTop <= 0) {
          clearInterval(this.timer);
          win.scrollTo(0, 0);

          this.$emit('hideShowTop')
        } else {
          win.scrollTo(0, scrollTop - 40);
        }
      }, 10);
    }
  },
  destroyed () {
    clearTimeout(this.timer);
  }
};
</script>

<style lang="less" scoped>
@import "../../assets/theme/themeBlue.less";
.scroll {
  border: 1px solid #d4d6d8;
  border-radius: 4px;
  background: #fff;
  z-index: 999;
  position: fixed;
  right: 55px;
  bottom: 69px;
  height: 40px;
  width: 40px;
  text-align: center;
  line-height: 40px;
  cursor: pointer;
  transition: all 0.1s ease-in-out;
  .icon-up {
    font-size: 20px;
  }
  &:hover {
    color: @primary;
  }
}
</style>
